package com.example.asus.homework11.activity;

import android.app.ActionBar;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.asus.homework11.BaseListner;
import com.example.asus.homework11.BaseModel;
import com.example.asus.homework11.R;
import com.example.asus.homework11.bean.LoginBean;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private Button bt_register,bt_login;
    private EditText editText_username,editText_password;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        sharedPreferences = getSharedPreferences("User",MODE_PRIVATE);
        initView();


    }

    void initView(){
        bt_register=(Button)findViewById(R.id.bt_register);
        bt_login=(Button)findViewById(R.id.bt_login);
        bt_register.setOnClickListener(this);
        bt_login.setOnClickListener(this);
        editText_username=findViewById(R.id.editText_username);
        editText_password=findViewById(R.id.editText_password);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case(R.id.bt_register):
                Intent intent = new Intent(getApplication(),RegisterActivity.class);
                startActivity(intent);
                break;
            case(R.id.bt_login):
                String pass=editText_password.getText().toString();
                final String user=editText_username.getText().toString();
                BaseModel<LoginBean> model = new BaseModel<>();
                model.callEnqueue(model.service.login(user, pass), new BaseListner<LoginBean>() {
                    @Override
                    public void onResponse(LoginBean loginBean) {
                        if(loginBean.getUserid().equals("0"))
                            Toast.makeText(getApplicationContext(),"登陆失败",Toast.LENGTH_SHORT).show();
                        else{
                            sharedPreferences.edit()
                                    .putString("userid",loginBean.getUserid())
                                    .putString("username",user)
                                    .apply();
                            Intent intent1 = new Intent(LoginActivity.this,HomeActivity.class);
                            startActivity(intent1);
                            finish();
                        }
                    }
                    @Override
                    public void onFail(String msg) {
                        Toast.makeText(getApplication(),msg,Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }
    }



}
